package com.demo.gulimall.ware.controller;

import com.demo.gulimall.common.utils.R;
import com.demo.gulimall.ware.service.WareSkuService;
import com.demo.gulimall.ware.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chen Yuan
 * @create 2023-05-29  13:34
 */
@Slf4j
@RestController
@RequestMapping("/ware/waresku")
public class WareSkuController {

	@Autowired
	private WareSkuService wareSkuService;


	@PostMapping("/hasstock")
	public R<List<SkuHasStockVo>> getSkuHasStock(@RequestBody List<Long> skuIds){
		List<SkuHasStockVo> skuStock = wareSkuService.getSkuHasStock(skuIds);
		return R.ok(skuStock);
	}

	/**
	 * 锁定库存
	 *
	 * 库存解锁场景:
	 * 1. 下订单成功, 但是支付取消
	 * 2. 下订单成功, 库存锁定成功, 但是后续操作异常
	 */
	@PostMapping("/lock/order")
	public R<Boolean> orderLockStock(@RequestBody SkuLockStockVo lockStockVo){
		try {
			Boolean locked = wareSkuService.orderLockStock(lockStockVo);
			return R.ok(locked);
		} catch (Exception e) {
			log.error("--> orderLockStock - 锁库存异常 - msg:{}", e.getMessage());
			return R.error(21,  e.getMessage());
		}
	}
}
